﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using WMM.Tapioca.Instance;
using WMM.Tapioca.Taxonomy;
using System.Xml;

namespace WMM.Tapioca.Formula.Filters
{
    /// <summary>
    /// The concept custom-attribute filter can be used to match facts based on the existence or value
    /// of a custom attribute in each concept's declaration.  A concept custom attribute is an attribute 
    /// on a concept declaration that is not in the XML Schema namespace or in the XBRL namespace . 
    /// </summary>
    public class ConceptCustomAttributeFilter : Filter
    {
        private string valueAttr;

        /// <returns> The required value for the custom attribute on the concept custom attribute filter. </returns>
        public string Value
        {
            get { return valueAttr; }
            set { valueAttr = value; }
        }

        private string qName;

        /// <returns> The attribute qname. </returns>
        public string QName
        {
            get { return qName; }
            set { qName = value; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public ConceptCustomAttributeFilter(Linkbase.Resource resource)
            : base(resource)
        {
            valueAttr = (string)resource.ResourceXElement.Attribute("value") ?? string.Empty;

            if (!string.IsNullOrEmpty(valueAttr))
            {
                valueAttr = EvaluateExpression(valueAttr);
            }

            XElement qnameX = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("attribute") &&
            d.Name.LocalName.Equals("qname")).FirstOrDefault();

            if (qnameX != null)
            {
                string qname = qnameX.Value;
                qname = qname.TrimEnd().TrimStart();
                qname = Regex.Replace(qname, @"\s+", " ");
                qName = qname;
            }   

            XElement qnameExpression = resource.ResourceXElement.Descendants().Where(d => d.Parent.Name.LocalName.Equals("attribute") &&
            d.Name.LocalName.Equals("qnameExpression")).FirstOrDefault();

            if (qnameExpression != null)
            {
                string expression = qnameExpression.Value;
                expression = expression.TrimEnd().TrimStart();
                expression = Regex.Replace(expression, @"\s+", " ");
                qName = EvaluateExpression(expression);
            }
        }


        public override List<FactItem> EvaluateFilter(VariableSet variableSet, List<FactItem> itemsToFilter, DTS dts)
        {
            this.dts = dts;

            List<FactItem> itemListTemp = new List<FactItem>();

            foreach (FactItem item in itemsToFilter)
            {
                foreach (XmlAttribute attr in item.Concept.SchemaElement.UnhandledAttributes)
                {
                    if (QName != null)
                    {
                        string customAttributeName = QName.Substring(QName.IndexOf(':') + 1);

                        if (attr.Name.Equals(QName))
                        {
                            if (Complement ? !(string.IsNullOrEmpty(Value) ? true : Value.Equals(attr.Value)) :
                                string.IsNullOrEmpty(Value) ? true : Value.Equals(attr.Value))
                            {
                                itemListTemp.Add(item);
                            }
                        }
                    }
                }
            }

            return itemListTemp;
        }
    }
}
